#include "include/shortcutdelegate.h"

#include "include/shortcutwidget.h"
#include <QSizePolicy>
#include <QKeySequence>
#include <QDebug>
#include <QFrame>
#include <QEvent>

#include "include/customshortcut.h"
#include "include/customshortcut_qmeta.h"



namespace
{
	const int UndefinedEditorRow = -1;


}


//to avoid mutable members in ShortcutDelegate
class SizeHintHelper
{

	public:

		SizeHintHelper()
			: _editorRow(UndefinedEditorRow)
		{
		}

//	int editorRow() const
//	{
//		return _editorRow;
//	}

		QWidget* editor()
		{
			return _editor;
		}

		void memorizeEditor(int rowIndex, QWidget* editor)
		{
			_editorRow = rowIndex;
			_editor = editor;
		}

		void resetEditor()
		{
			_editorRow = UndefinedEditorRow;
			_editor = 0;
		}

		QWidget* getEditor(int rowIndex)
		{
			return rowIndex == _editorRow ? _editor : 0;
		}

		QWidget* getEditor(const QModelIndex& index)
		{
			if (!index.isValid())
				return 0;

			return getEditor(index.row());
		}

	private:

		int _editorRow;
		QPointer<QWidget> _editor;
};



ShortcutDelegate::ShortcutDelegate(QObject *parent) :
	QStyledItemDelegate(parent)
	, _sizeHintHelper(new SizeHintHelper())
{
}


ShortcutDelegate::~ShortcutDelegate()
{
	delete _sizeHintHelper;
	_sizeHintHelper = 0;
}


QWidget* ShortcutDelegate::createEditor(QWidget *parent,
                                        const QStyleOptionViewItem &,
                                        const QModelIndex &index) const
{
	ShortcutWidget* widget = new ShortcutWidget(parent);
	widget->setAutoFillBackground(true);

	_sizeHintHelper->memorizeEditor(index.row(), widget);

	//connect(widget, SIGNAL(destroyed()), this, SLOT(onEditorDestroyed()));

	return widget;
}


void ShortcutDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const
{
	if (!index.isValid())
		return;

	ShortcutWidget* widget = static_cast<ShortcutWidget*>(editor);

	QMap<QString, QVariant> map = index.data(Qt::EditRole).toMap();

	if (!map.isEmpty())
	{
		QKeySequence defaultSequence = ShortcutsMetaHelper::getDefaultShortcut(map);
		QKeySequence currentSequence = ShortcutsMetaHelper::getCurrentShortcut(map);
		widget->setSequences(defaultSequence, currentSequence);
	}

	emit editorDisplayed(index.row());
}


void ShortcutDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
	if (!index.isValid())
		return;

	ShortcutWidget* widget = static_cast<ShortcutWidget*>(editor);

	QMap<QString, QVariant> map;
	ShortcutsMetaHelper::setCurrentShortcut(map, widget->currentSequence());

	model->setData(index, QVariant(map), Qt::EditRole);

	_sizeHintHelper->resetEditor();
	emit editorClosed(index.row());
}


QSize ShortcutDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
	ShortcutWidget* widget = qobject_cast<ShortcutWidget*>(_sizeHintHelper->getEditor(index));

	if (widget == 0)
		return QStyledItemDelegate::sizeHint(option, index);

	return QSize(option.decorationSize.width(), widget->sizeHint().height());
}



void ShortcutDelegate::writeEditorToModel()
{
	if (!_sizeHintHelper->editor())
		return;

	emit commitData(_sizeHintHelper->editor());
}


//void ShortcutDelegate::onEditorDestroyed()
//{
//	int rowIndex = _sizeHintHelper->editorRow();
//	_sizeHintHelper->resetEditor();
//	emit editorClosed(rowIndex);
//}
